package edu.kit.csl.tests.pisa.models;

/*
This file is part of the PISA Alignment Tool.

Copyright (C) 2013
Karlsruhe Institute of Technology
Cognitive Systems Lab (CSL)
Felix Stahlberg

PISA is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PISA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PISA. If not, see <http://www.gnu.org/licenses/>.
*/

import static org.junit.Assert.*;
import junit.framework.Assert;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import edu.kit.csl.pisa.datatypes.SentenceAlignment;
import edu.kit.csl.pisa.models.AlignmentModel;
import edu.kit.csl.pisa.ui.Configuration;

/**
 * Test cases for {@link edu.kit.csl.pisa.models.AlignmentModel}.
 */
public class AlignmentModelTest {
	
	private static final double DELTA = 0.1e-10;
	
	private static class AlignmentTestModel extends AlignmentModel {
		public AlignmentTestModel(String name) {
			super(name);
		}
		@Override
		public double calculateAlignmentProbability(SentenceAlignment a) {
			return 0;
		}
		public void normalizeWrapper(double a[]) {
			normalize(a);
		}
		public double binomSpecialWrapper(int k, int phi) {
			return binomSpecial(k, phi);
		}
		@Override
		public void initializeFractionalCounts() {
			fail("Should not be called.");
		}

		@Override
		public void writeBackFractionalCounts() {
			fail("Should not be called.");
		}

		@Override
		public void aggregateFractionalCount(SentenceAlignment a, double weight) {
			fail("Should not be called.");
		}
		@Override
		public void dumpToFilesystem(String prefix, String postfix) {
			fail("Should not be called.");
		}
	}
	private static AlignmentTestModel model;

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		Configuration.getSingleton().set("model3Iterations", 2);
		Configuration.getSingleton().set("model3SuccStrategy", "id");
		Configuration.getSingleton().set(
				"model3CrossoverProb", 0.1f);
		model = new AlignmentTestModel("model3");
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		model = new AlignmentTestModel("model3");
	}

	/**
	 * Test method for {@link edu.kit.csl.pisa.models.AlignmentModel#binomSpecial(int, int)}.
	 */
	@Test
	public final void testBinomSpecial() {
		Assert.assertEquals(Double.NEGATIVE_INFINITY,
				model.binomSpecialWrapper(3, 2), DELTA);
		Assert.assertEquals(0d,
				model.binomSpecialWrapper(3, 0), DELTA);
		Assert.assertEquals(0d,
				model.binomSpecialWrapper(0, 0), DELTA);
		Assert.assertEquals(Math.log(495),
				model.binomSpecialWrapper(20, 8), DELTA);
		Assert.assertEquals(Math.log(58905),
				model.binomSpecialWrapper(40, 4), DELTA);
	}

	/**
	 * Test method for {@link edu.kit.csl.pisa.models.AlignmentModel#importModelParameters(edu.kit.csl.pisa.models.AlignmentModel)}.
	 */
	@Test
	public final void testImportModelParameters() {
		try {
			model.importModelParameters(null);
		} catch (IllegalArgumentException e) {
			return;
		}
		fail("No IllegalArgumentException thrown.");
	}

	/**
	 * Test method for {@link edu.kit.csl.pisa.models.AlignmentModel#getName()}.
	 */
	@Test
	public final void testGetName() {
		Assert.assertEquals("model3", model.getName());
	}
	
	/**
	 * Test method for getConfig*() methods.
	 */
	@Test
	public final void testGetConfig() {
		Assert.assertEquals(new Integer(2),
				model.getConfigInteger("Iterations"));
		Assert.assertEquals("id", model.getConfigString("SuccStrategy"));
		Assert.assertEquals(0.1f, model.getConfigFloat(
				"CrossoverProb"));
	}

	/**
	 * Test method for {@link edu.kit.csl.pisa.models.AlignmentModel#normalize(double[])}.
	 */
	@Test
	public final void testNormalize() {
		double[] testArr = {0, 8, 1, 5};
		model.normalizeWrapper(testArr);
		assertArrayEquals(new double[]{
				Double.NEGATIVE_INFINITY,
				Math.log(8.0d/14.0d),
				Math.log(1.0f/14.0d),
				Math.log(5.0d/14.0d)
		}, testArr, DELTA);
	}
}
